package com.itboye.ihomebank.me;

import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.login.TimeCount;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 绑定手机啊
 * 
 */
public class ActivityBindPhone extends BaseOtherActivity implements Observer {
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	private EditText bindphone_phone;// 手机号
	private EditText edit_yzm;// 验证码
	private EditText bindphone_pass;//密码
	private TextView bindphone_huoqu;// 获取验证码
	private TextView bindphone_ok;// 提交
	UserPresenter userPresenter;
	boolean isChange = true;
	private String code;
	private String phone;
	private String uid;
	private String pass;
	
	private TimeCount time;// 倒计时

	@Override
	public void onClick(View v) {
		phone = bindphone_phone.getText().toString();
		code=edit_yzm.getText().toString();
		pass=bindphone_pass.getText().toString();
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.bindphone_huoqu:// 获取验证码
			if (phone.equals("")) {
				ByAlert.alert("请输入手机号码");
				return;
			}
			isChange = (boolean) SPUtils.get(this, null,
					SPContants.IS_BIND_PHONE, false);
			if (isChange) {
				userPresenter.send(phone, "3");
			} else {
				userPresenter.send(phone, "4");
			}
			time.start();
			break;
		case R.id.bindphone_ok:// 提交认证
			if (code == null) {
				ByAlert.alert("验证码有误");
				return;
			}
			isChange = (boolean) SPUtils.get(this, null,
					SPContants.IS_BIND_PHONE, false);
			uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
			userPresenter.bindPhone(uid, phone, code,pass,false);
			break;

		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_bindphone;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("绑定手机");
		userPresenter = new UserPresenter(this);
		
		time = new TimeCount(60000, 1000, bindphone_huoqu);// 构造CountDownTimer对象

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.send_success) {
				ByAlert.alert(entity.getData());
				code = entity.getData() + "";
			} else if (entity.getEventType() == UserPresenter.send_fail) {
				ByAlert.alert(entity.getData());
			}
			
			if (entity.getEventType() == UserPresenter.updateorbindphone_success) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.updateorbindphone_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
